package com.huahua.api.controller;

import com.huahua.api.base.R;
import com.huahua.api.entity.vo.ip.IpVo;
import com.huahua.api.util.CusAccessObjectUtil;
import com.huahua.api.util.IPUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author LoveHuahua
 * @date 2021年09月11日 14:49
 * @description believe in yourself
 */
@RestController
@RequestMapping("/ip")
public class IpController {

    @RequestMapping("/ip2long")
    public R<IpVo> getInfo(String ip) {
        String address = IPUtil.getAddress(ip);
        IpVo ipVo = new IpVo();
        ipVo.setIp(ip);
        ipVo.setAddress(address);
        return R.success(ipVo);
    }

    @PostMapping("/getCurrentIp")
    public R<IpVo> getCurrentIp(HttpServletRequest request) {
        String ipAddress = CusAccessObjectUtil.getIpAddress(request);
        String address = IPUtil.getAddress(ipAddress);
        IpVo ipVo = new IpVo();
        ipVo.setIp(ipAddress);
        ipVo.setAddress(address);
        return R.success(ipVo);
    }
}
